/**
 * 判断url是否是http或https
 * @param {string} path
 * @returns {boolean}
 */
export function isHttp(url) {
  return url.includes('http://') || url.includes('https://')
}

/**
 * 判断path是否为外链
 * @param {string} path
 * @returns {boolean}
 */
export function isExternal(path) {
  return /^(?:https?:|mailto:|tel:)/.test(path)
}

/**
 * @description 验证用户名
 * @param {string} str
 * @returns {boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.includes(str.trim())
}

/**
 * @param {string} url
 * @returns {boolean}
 */
export function isURL(url) {
  const reg = /^(?:https?|ftp):\/\/(?:[a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|[1-9])\.(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|\d)\.(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|\d)\.(25[0-5]|2[0-4]\d|[01]\d{2}|[1-9]\d|\d)|localhost|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:\d+)*(\/($|[\w.,?'\\+&%$#=~-]+))*$/
  return reg.test(url)
}

/**
 * @description 验证是否包含空格
 * @param {string} str
 * @returns {boolean}
 */
export function validLowerCase(str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {boolean}
 */
export function validUpperCase(str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/**
 * @param {string} str
 * @returns {boolean}
 */
export function validAlphabets(str) {
  const reg = /^[A-Z]+$/i
  return reg.test(str)
}

/**
 * @param {string} email
 * @returns {boolean}
 */
export function isEmail(email) {
  const reg = /^[\w.!#$%&’*+/=?^`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9-]+)*$/i
  return reg.test(email)
}

/**
 * @param {string} str
 * @returns {boolean}
 */
export function isString(str) {
  return typeof str === 'string' || str instanceof String
}

/**
 * @param {Array} arg
 * @returns {boolean}
 */
export function isArray(arg) {
  if (typeof Array.isArray === 'undefined') {
    return Object.prototype.toString.call(arg) === '[object Array]'
  }
  return Array.isArray(arg)
}

/**
 * @description 验证身份证号码
 * @param {string} num - 身份证号
 * @returns {boolean}
 */
export function isIdCardNo(num) {
  const reg = /^\d{15}$|^\d{18}$|^\d{17}[\dX]$/
  return reg.test(num)
}

/**
 * @description 验证是否为对象
 * @param {object} obj
 * @returns {boolean}
 */
export function isObject(obj) {
  return obj !== null && typeof obj === 'object'
}

/**
 * @description 判断是否是正则表达式
 * @param {RegExp} reg
 * @returns {boolean}
 */
export function isRegExp(reg) {
  return reg instanceof RegExp || (typeof reg === 'string' && reg.match(/^\/(.+)\/([a-z]*)$/))
}

export function isValidCreditCode(code) {
  // 正则表达式匹配统一社会信用代码
  const creditCodeRegex = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/
  return creditCodeRegex.test(code)
}
/**
 * @description 验证是否为手机号码
 * @param {string} mobile 手机号码
 * @returns {boolean}
 */
export function isValidMobile(mobile) {
  const mobileRegex = /^1[34578]\d{9}$/
  return mobileRegex.test(mobile)
}
